package com.cmfi.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cmfi.dao.ForkliftQualityDao;
import com.cmfi.domain.AssembleQuality;
import com.cmfi.service.inter.ForkliftQualityService;

/**
 * Service Implementation for managing {@link AssembleQuality}.
 */
@Service
public class ForkliftQualityServiceImpl implements ForkliftQualityService {

    private final Logger log = LoggerFactory.getLogger(ForkliftQualityServiceImpl.class);

    @Autowired
    private ForkliftQualityDao forkliftQualityRepository;
    
//    @Autowired
//    private final ForkliftSnapRepository forkliftSnapRepository;

	@Override
	public AssembleQuality save(AssembleQuality forkliftQuality) {
		log.info("[ForkliftQualityServiceImpl][save] ForkliftQuality: {}", forkliftQuality);
		forkliftQualityRepository.save(forkliftQuality);
		return forkliftQuality;
	}

	public List<AssembleQuality> submit(List<AssembleQuality> forkliftQualities) {
		log.info("[ForkliftQualityServiceImpl][submit] forkliftQualities: {}", forkliftQualities);
		List<AssembleQuality> qualitys = new ArrayList<>();
		for (AssembleQuality qu : forkliftQualities) {
			update(qu.getId(), qu);
			qualitys.add(qu);
		}
		return qualitys;
	}

	public List<AssembleQuality> getQualityByVinCode(String vinCode) {
		log.info("[ForkliftQualityServiceImpl][getQualityByVinCode] vinCode:{}", vinCode);
		return forkliftQualityRepository.findForkliftQualitiesByVinCode(vinCode);
	}

    public  List<AssembleQuality> getQualityByPosition(String position){
        log.debug("Request to get ForkliftQuality, position : {}", position);
//        return  forkliftSnapRepository.getForkliftSnapByPosition(position)
//            .map(ForkliftSnap::getVinCode)
//            .flatMapMany(
//                vinCode -> forkliftQualityRepository.getForkliftQualitiesByVinCodeAndPosition(vinCode,position)
//            );
    	return forkliftQualityRepository.findQualityByPosition(position);
    }

	@Override
	public AssembleQuality update(Long id, AssembleQuality forkliftQuality) {
		forkliftQuality.setId(id);
		log.info("[ForkliftQualityServiceImpl][update] forkliftQuality:{}", forkliftQuality);
		forkliftQualityRepository.update(forkliftQuality);
		return forkliftQuality;
	}

	@Override
	public AssembleQuality partialUpdate(Long id, AssembleQuality forkliftQuality) {
		log.info("[ForkliftQualityServiceImpl][update] forkliftQuality:{}", forkliftQuality);
		forkliftQuality.setId(id);
		AssembleQuality existingForkliftQuality = forkliftQualityRepository.findById(forkliftQuality.getId());
//		if (forkliftQuality.getVinCode() != null) {
//			existingForkliftQuality.setVinCode(forkliftQuality.getVinCode());
//		}
//		if (forkliftQuality.getPartCode() != null) {
//			existingForkliftQuality.setPartCode(forkliftQuality.getPartCode());
//		}
//		if (forkliftQuality.getPartName() != null) {
//			existingForkliftQuality.setPartName(forkliftQuality.getPartName());
//		}
//		if (forkliftQuality.getPartType() != null) {
//			existingForkliftQuality.setPartType(forkliftQuality.getPartType());
//		}
//		if (forkliftQuality.getPosition() != null) {
//			existingForkliftQuality.setPosition(forkliftQuality.getPosition());
//		}
//		if (forkliftQuality.getUsername() != null) {
//			existingForkliftQuality.setUsername(forkliftQuality.getUsername());
//		}
		if (forkliftQuality.getQuality() != null) {
			existingForkliftQuality.setQuality(forkliftQuality.getQuality());
		}
		if (forkliftQuality.getQualityCodeLevelOne() != null) {
			existingForkliftQuality.setQualityCodeLevelOne(forkliftQuality.getQualityCodeLevelOne());
		}
		if (forkliftQuality.getQualityCodeLevelTwo() != null) {
			existingForkliftQuality.setQualityCodeLevelTwo(forkliftQuality.getQualityCodeLevelTwo());
		}
		if (forkliftQuality.getQualityCodeLevelThree() != null) {
			existingForkliftQuality.setQualityCodeLevelThree(forkliftQuality.getQualityCodeLevelThree());
		}
		if (forkliftQuality.getDescription() != null) {
			existingForkliftQuality.setDescription(forkliftQuality.getDescription());
		}
		if (forkliftQuality.getCreateTime() != null) {
			existingForkliftQuality.setCreateTime(forkliftQuality.getCreateTime());
		}
		if (forkliftQuality.getUpdateTime() != null) {
			existingForkliftQuality.setUpdateTime(forkliftQuality.getUpdateTime());
		}
		forkliftQualityRepository.update(existingForkliftQuality);
		return existingForkliftQuality;
	}

	@Override
	public List<AssembleQuality> findAll() {
		log.info("[ForkliftQualityServiceImpl][update] getting.");
		return forkliftQualityRepository.findAll();
	}

    public Long countAll() {
        return forkliftQualityRepository.count();
    }

    @Override
    public AssembleQuality findOne(Long id) {
        log.debug("[ForkliftQualityServiceImpl][update] id:{}", id);
        return forkliftQualityRepository.findById(id);
    }

	@Override
	public void delete(Long id) {
		log.debug("[ForkliftQualityServiceImpl][update] id:{}", id);
		forkliftQualityRepository.deleteById(id);
	}
	
	public List<AssembleQuality> findByPage(Long pageNum, Long pageSize) throws Exception {
		log.info("[ForkliftQualityServiceImpl][update] pageNum:{},pageSize:{}", pageNum, pageSize);
		Long allNum = forkliftQualityRepository.count();
		long start = 0;
		if (pageNum == 0 && pageSize == 0) {
			start = 0;
			pageSize = allNum;
		} else {
			start = (pageNum - 1) * pageSize;
			if (start > allNum) {
				throw new Exception("请求的页已经不存在");
			}
		}
		return forkliftQualityRepository.findAllByPage(start, pageSize);
	}

}
